<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: UserGroup.php 262 2011-07-23 15:51:14Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

//class Permission extends Baogg_Db_Table {
class UserGroup  extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db ;
	protected $_primary; 
	
	function __construct() {
		$this->_name =   'user_group';
		
		$this->myPrimary=$this->primary= $this->_primary='user_group_id';
		parent::__construct();
	}
	
	
	

	function getSame($arr) {	
		$ret=array();
		
		$where['user_group_name']=$arr['user_group_name'];
		return parent::getSame($where);
	
	}
	
	
 
	function updatePids()
    {
    	$pid=0;$pid_value=',';$pid_col='user_group_pid';
    	$this->_db->query("UPDATE {$this->_name} SET {$pid_col}s=''");
    	$this->_db->query("UPDATE {$this->_name} SET {$pid_col}s=concat('{$pid_value}',{$this->myPrimary},',') WHERE {$pid_col}={$pid}");
    	$sql="UPDATE {$this->_name} a
					  JOIN {$this->_name} b
					    ON a.{$pid_col} = b.{$this->myPrimary}
					SET a.{$pid_col}s = CONCAT(b.{$pid_col}s,a.{$this->myPrimary},',')
					WHERE b.{$pid_col}s != '' and a.{$pid_col}s=''";
    	while($this->_db->query($sql)->rowCount()){
    		//echo '<pre>';print_r($affected->rowCount());exit;
		}
		
		return true;
    }
    
    
    /**
     * 
     * get ids with user group
     * @param array $arr_group_id
     */
    function getWithChildIds($arr_group_id=array())
    {
    	if(!$arr_group_id){
    		return array();
    	}
    	
    	$arr_group_id=array_filter((array)$arr_group_id);
    	$where=array();
    	foreach((array)$arr_group_id as $k=>$v){
			$where[]="user_group_pids LIKE '%,{$v},%'";					
		}
		if($where){
			$where=implode(" or ",$where);
			$rs_user_group=$this->getList(array($where));
			foreach((array)$rs_user_group as $k=>$v){
				$arr_group_id[]=$v['user_group_id'];
			}			
		}
		return array_unique((array)$arr_group_id);
    }
    
    
    /**
     * 
     * generate tree use group
     * @param array $arr_group_id
     */
    function getWithChild($arr_group_id=array())
    {    	
    	if(!$arr_group_id){
    		return array();
    	}
    	    	
    	$arr_group_id=array_filter($arr_group_id);
    	foreach((array)$arr_group_id as $k=>$v){
			$where[]="user_group_pids like '%,{$v},%'";					
		}
		$rs_user_group=array();
		if($where){
			$where=array("(".implode(" or ",$where).")");
			$where[]="state=1";
			$where=implode(" and ",$where);
			//echo '<pre>';print_r($where);exit;
			//$rs_user_group=$this->getList(,null,null,array("user_group_id","user_group_name as user_group_name2","user_group_pid","*"));
			$select=$this->_db->select();		
			$select->from($this->_name,array("user_group_id","user_group_name as user_group_name2","user_group_pid","*"));
			$select->where($where);
			$rs_user_group=$this->_db->query($select)->fetchAll();
			
			foreach((array)$rs_user_group as $k=>$v){
				//change top group's parent id to 0
				if(in_array($v['user_group_id'],$arr_group_id)){
					$arr_pids=explode(",",trim($v['user_group_pids'],','));
					$arr_intersect_pids=array_intersect($arr_group_id, $arr_pids);
					//is not child of arr_group_id
					if(count($arr_intersect_pids)==1){
						$rs_user_group[$k]['user_group_pid']=0;
					}
				}				
			}			
		}		
		
		return $rs_user_group;
    }
    
    
    function getTreeByIds($arr_group_id=array()){
    	$ret=array();
    	
    	$arr_group_id=$this->getWithChildIds($arr_group_id);
    	
    	
    	$where_group['user_group_id']=$arr_group_id;
    	//get user group with sub group
    	
    	
    	$tr_group=$this->getComboTree($where_group,null,null,array("user_group_id","user_group_name as user_group_name2","user_group_pid","*"));
    	
    	return $tr_group;
    	
    }
    
    function getTopGroupById($user_group_id=0){
    	$rs=$this->getOne($user_group_id);
    	$arr=explode(",",$rs[0]['user_group_pids']);
    	return (int)@$arr[2];
    }
}
